package com.mashibing.servicepassageruser.service.Impl;
import java.time.LocalDateTime;
import java.util.Date;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.servicepassageruser.dto.PassengerUser;
import com.mashibing.servicepassageruser.mapper.PassengerUserMapper;
import com.mashibing.servicepassageruser.service.UserService;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:
 * @create: 2024/3/25 17:27
 * @description:
 */
@Service
public class UserServiceImpl extends ServiceImpl<PassengerUserMapper, PassengerUser> implements UserService {

    @Autowired
    private PassengerUserMapper passengerUserMapper;

    @Override
    public ResponseResult<String> loginOrRegister(String passengerPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> userList = passengerUserMapper.selectByMap(map);
        if (userList.isEmpty()) {
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerGender(false);
            passengerUser.setState(false);
            passengerUser.setGmtCreate(LocalDateTime.now());
            passengerUser.setGmtModified(LocalDateTime.now());
            passengerUserMapper.insert(passengerUser);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<PassengerUser> getUser(String passengerPhone) {
        // 根据手机号查询用户信息
        List<PassengerUser> passengerUsers = passengerUserMapper.selectList(new QueryWrapper<PassengerUser>()
                .eq("passenger_phone", passengerPhone));
        if (CollectionUtils.isEmpty(passengerUsers)) {
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(),CommonStatusEnum.USER_NOT_EXISTS.getValue());
        }
        PassengerUser passengerUser = passengerUsers.get(0);
        return ResponseResult.success(passengerUser);
    }
}
